<?php
/**
 * 学生控制器
 *
 * @package    app
 * @version    1.0
 * @author     Ray 33705910@qq.com
 * @license    MIT License
 * @copyright  2013 - 2015 Ray
 * @link       http://wangxiaolei.cn
 */

/**
 * 本控制器主要用于：
 * 1.员工管理
 * @package  app
 * @extends  Controller
 */
namespace admin;

class Controller_Peoples extends Controller_BaseController {

	public $template = 'ace/template';

	public function before(){

        parent::before();
		
        $params = array(
            'controller_name' => '人员管理'
        );
        
        \View::set_global($params);
    }

    /*
    * 员工列表
    */
	public function action_index(){
		$params = array(
  			'title' => '人员列表',
  			'menu' => 'people-list',
  			'action_name' =>'人员列表'
  		);

		$keyword =  \Input::get('keyword','');
		if(isset($keyword) == ''){
			$items = \Model_People::query()->related(array('photoalbum'));
		}else{
			$items = \Model_People::query()->related(array('photoalbum'))->or_where('last_name', 'LIKE', "%{$keyword}%")->or_where('phone','LIKE',"%{$keyword}%");
		}
		
        //分页查询
        $count = $items->count();
        $config = array(
            'pagination_url' => "/admin/peoples?action=people",
            'total_items'    => $count,
            'per_page'       => \Input::get('count', 10),
            'uri_segment'    => 'start',
            'show_first'     => true,
            'show_last'      => true,
            'name'           => 'bootstrap3_cn' . (\Input::is_ajax() ? '_ajax' : '')
        );

        $pagination = new \Pagination($config);
        $params['pagination'] = $pagination;        
        $params['items'] = $items
                            ->rows_offset($pagination->offset)
                            ->rows_limit($pagination->per_page)
                            ->get();

  		\View::set_global($params);
		$this->template->content = \View::forge("ace/peoples/index");
	}

	/**
	* 编辑/新增员工信息
	*
	* @param $id int 员工ID
	* @return
	*/
	public function action_create($id=0){		
		$params = array(
  			'title' => $id == 0 ? '添加人员信息' :'修改人员信息',
  			'menu' => 'people-add',
  			'action_name' => $id == 0 ? '添加人员信息' :'修改人员信息'
  		);	

  		if(! \Security::check_token()){
  			$params['item'] = \Model_People::query()->where(array('id' => $id))->get_one();
			\View::set_global($params);
			$this->template->content = \View::forge("ace/peoples/details");
        }

		if(\Input::method() == 'POST')
		{
			$isSave = true;
			$errContent = '';
			$data = \Input::post();

			if($id)
			{
				$people = \Model_People::query()->where(array('id' => $id))->get_one();
				$people->set($data);

			} else {
				//检测手机号码是否注册过				
				$user = \Model\Auth_User::query()->where(array('username' => $data['phone']))->get_one();

				if($user){
					$isSave = false;
			        $errContent = urlencode("系统错误，手机号码已被占用!");			        
		            //return json_encode(array('status' => 'error', 'msg'=>$errContent));
				}

				$user =\Model\Auth_User::query()->where(array('email' => $data['email']))->get_one();
				if($user){
					$isSave = false;					
			        $errContent = urlencode("系统错误，邮箱已被占用...");
			        //return json_encode(array('status' => 'error', 'msg'=>$errContent));
				}

				$flag = \Auth::create_user($data['phone'], '5201314', $data['email'], 7);
				$data['user_id'] = $flag;
				$people = \Model_People::forge($data);				
			}


			if($isSave)
			{
				if($people->save()){
					//die(json_encode(array('status' => 'success', 'msg' => '保存成功！')));
					\Session::set_flash('msg', array('status' => 'succ', 'msg' => '保存成功！'));					
				}else{
					//die(json_encode(array('status' => 'err', 'msg' => '保存失败！')));
					\Session::set_flash('msg', array('status' => 'err', 'msg' => '保存失败！'));					
				}				
			}else{
				\Session::set_flash('msg', array('status' => 'err', 'msg' => '操作失败'));
			}            
		}

		$params['item'] = \Model_People::query()->where(array('id' => $id))->get_one();
		\View::set_global($params);
		$this->template->content = \View::forge("ace/peoples/details");
	}


	/**
	* 删除一个职员
	*
	* @param $id int 职员ID
	* @return
	*/
	public function action_delete($id){
		if( ! $id){
			if(\Input::is_ajax()){
				die(json_encode(array('status' => 'err', 'msg' => '无效的参数', 'errcode' => 10)));
			}
		}

		$employee = \Model_People::find($id);
		if( ! $employee){
			if(\Input::is_ajax()){
				die(json_encode(array('status' => 'err', 'msg' => '无效的参数', 'errcode' => 11)));
			}
		}

		$employee->is_delete = true;
		if($employee->save()){
			if(\Input::is_ajax()){
				die(json_encode(array('status' => 'succ', 'msg' => '', 'errcode' => 0)));
			}
		}else{
			if(\Input::is_ajax()){
				die(json_encode(array('status' => 'err', 'msg' => '操作失败', 'errcode' => 10)));
			}
		}
	}

}
